package com.zx.map.service.impl;

import com.zx.map.service.EncashLogService;
import com.zx.map.service.RiskControlService;
import com.zx.map.service.model.EncashLogModel;
import com.zx.map.service.model.MerchantInfoDetailModel;
import com.zx.map.service.model.RiskControlModel;
import com.zx.map.utils.SUConstants;
import com.zx.mobile.MobileCommonHelper;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by IntelliJ IDEA. User: Administrator Date: 14-3-1 Time: 上午11:07 To
 * change this template use File | Settings | File Templates.
 */
@Component("riskControlService")
public class RiskContolServiceImpl implements RiskControlService {
    /**
     * log
     */
    private static Logger log = Logger
            .getLogger(RiskContolServiceImpl.class);

    /**
     * 扫码充值 未实名认证 及待审核用户 额度准许设定
     */
    public static final double INCOMELIMITFORNOTAUTH = 200;
    /**
     * 余额取现单笔最高额度
     */
    public static final double ENCHASHLIMIT = 50000;
    /**
     * 余额取现时间段内次数限制 10分钟
     */
    public static final int T0ENCHASHTIMELIMIT = 10;

    @Autowired
    private EncashLogService encashLogService;

    /**
     * 业务许可 限额风险控制
     *
     * @param transType
     * @param transAmt
     * @param merchantInfoDetailModel
     * @return
     * @throws DataAccessException
     */
    @Override
    public RiskControlModel bussinessAmtRiskControl(String transType, String transAmt, MerchantInfoDetailModel merchantInfoDetailModel) throws DataAccessException {
        RiskControlModel riskControlModel = new RiskControlModel();
        //扫码充值
        if (SUConstants.TRANSTYPE_INCOME.equals(transType)) {
            String status = merchantInfoDetailModel.getStatus();
            //对未实名认证用户进行金额限制
            if (MobileCommonHelper.APPUSERSTATUS_WAIT.equals(status) || MobileCommonHelper.APPUSERSTATUS_NOTREQUEST.equals(status)) {
                if (Double.parseDouble(transAmt) > INCOMELIMITFORNOTAUTH) {
                    riskControlModel.setRiskCode("9999");
                    riskControlModel.setRiskMsg("未实名认证用户单笔不能超过" + INCOMELIMITFORNOTAUTH + "元");
                }
            }
        } else if (SUConstants.TRANSTYPE_T0ENCASH.equals(transType) || SUConstants.TRANSTYPE_T1ENCASH.equals(transType))//余额取现
        {
            if (Double.parseDouble(transAmt) > ENCHASHLIMIT) {
                riskControlModel.setRiskCode("9999");
                riskControlModel.setRiskMsg("余额取现单笔不能超过" + ENCHASHLIMIT + "元");
            }
        } else if (SUConstants.TRANSTYPE_CREDITENCASH.equals(transType))//信用卡还款
        {
            if (Double.parseDouble(transAmt) > ENCHASHLIMIT) {
                riskControlModel.setRiskCode("9999");
                riskControlModel.setRiskMsg("信用卡还款单笔不能超过" + ENCHASHLIMIT + "元");
            }
        }
        return riskControlModel;
    }

    /**
     * 业务许可 限次风险控制
     *
     * @param transType
     * @param transAmt
     * @param merchantInfoDetailModel
     * @return
     * @throws DataAccessException
     */
    @Override
    public RiskControlModel bussinessTimeRiskControl(String transType, String transAmt, MerchantInfoDetailModel merchantInfoDetailModel) throws DataAccessException {
        RiskControlModel riskControlModel = new RiskControlModel();
        //T0实时业务进行限制
        if (SUConstants.TRANSTYPE_T0ENCASH.equals(transType)) {
            //查询在该时间段内 用户是否存在成功T0业务 金额相同
            EncashLogModel encashLogModel = new EncashLogModel();
            encashLogModel.setEncashStatus(MobileCommonHelper.OUTCOMESTATUS_SUCCESS);
            encashLogModel.setMerchantId(merchantInfoDetailModel.getMerchantId());
            encashLogModel.setEncashAmt(Double.parseDouble(transAmt));
            encashLogModel.setEncashType("0"); //设置取现类型
            List<EncashLogModel> encashLogModelList = encashLogService.getListPeriodByModel(encashLogModel, T0ENCHASHTIMELIMIT);
            //对未实名认证用户进行金额限制
            if (null != encashLogModelList && encashLogModelList.size() >= 1) {
                riskControlModel.setRiskCode("9999");
                riskControlModel.setRiskMsg("实时取现短时间内不允许出现重复金额");
            }
        }
        return riskControlModel;
    }
}
